//
//  WJYPhotosViewController.m
//  WWLNetease
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "WJYPhotosViewController.h"

@interface WJYPhotosViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *photosArr;

@property (nonatomic, retain) UICollectionView *imageCollectionView;
@property (nonatomic, retain) UILabel *textLabel;
@property (nonatomic, retain) NSArray *photos;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UIImageView *backGroundImage;
@property (nonatomic, retain) UIBlurEffect *effect;
@property (nonatomic, retain) UIVisualEffectView *visEffect;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UILabel *numL;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic, retain) UIImageView *myView;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) CGFloat dy;

@end

@implementation WJYPhotosViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    [self handelData];
    [self creatImageCollectionView];
    [self creatTextLabel];
    [self createNumLabel];
    

    self.navigationItem.title = self.photoTitle;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)handelData {
    
    NSString *str = [[self.photosetID substringFromIndex:4] stringByReplacingOccurrencesOfString:@"|" withString:@"/"];
    str = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@.json", str];
    
    [AppTools getDataWithURL:str Block:^(id result) {
        
        self.photos = [result objectForKey:@"photos"];
        [self.imageCollectionView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error : %@", error);
    }];
    
    
    
}

-(void)creatImageCollectionView {
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.itemSize = CGSizeMake(SCREEN_FRAME.size.width, SCREEN_FRAME.size.height);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    self.imageCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_FRAME.size.width, SCREEN_FRAME.size.height) collectionViewLayout:flow];
    self.imageCollectionView.backgroundColor = [UIColor clearColor];
    self.imageCollectionView.delegate = self;
    self.imageCollectionView.dataSource = self;
    self.imageCollectionView.pagingEnabled = YES;
    [self.view addSubview:self.imageCollectionView];
    [self.imageCollectionView registerClass:[WJYPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCollectionViewCell"];
    
}

- (void)creatTextLabel {
    
    self.myView = [[UIImageView alloc]initWithFrame:CGRectMake(5, SCREEN_FRAME.size.height / 1.5 + 30, SCREEN_FRAME.size.width - 2 * 5, SCREEN_FRAME.size.height - SCREEN_FRAME.size.height / 1.3)];
    self.myView.alpha = 0.5;
    self.myView.userInteractionEnabled = YES;
    UIPanGestureRecognizer *longPress = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
    [self.myView addGestureRecognizer:longPress];
    [self.view addSubview:self.myView];
    
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.numberOfLines = 0;
    self.textLabel.textColor = [UIColor whiteColor];
    self.textLabel.backgroundColor = [UIColor clearColor];
    
    [self.myView addSubview:self.textLabel];
    
}

- (void) createNumLabel
{
    self.numL = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, 100, 30)];
    self.numL.textColor = [UIColor whiteColor];
    [self.view addSubview:self.numL];
}

- (void)longAction:(UILongPressGestureRecognizer *)longPress{


    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        // 获取手指触摸屏幕的点
        self.startPoint =  [longPress locationInView:self.myView];

        
    }
    if (longPress.state == UIGestureRecognizerStateChanged) {

        
        // 获取手指触摸屏幕的点
        CGPoint point =  [longPress locationInView:self.myView];
//        NSLog(@"-----point = %@", NSStringFromCGPoint(point));
        
        // 计算x轴移动的距离
//        CGFloat dx = point.x - self.startPoint.x;
        
        // 计算y轴移动的距离
        self.dy = point.y - self.startPoint.y;
        
        // 改变中心点得坐标
        self.myView.center = CGPointMake(self.myView.center.x , self.myView.center.y + self.dy);
        
        
    }
    if (longPress.state == UIGestureRecognizerStateEnded) {
        
        
        
        if (self.myView.frame.origin.y + self.dy < 667 - self.rect.size.height) {
            
            self.myView.center = CGPointMake(self.myView.frame.size.width / 2 + 8, (667 - self.rect.size.height));
            
        }
        
        if ((self.myView.frame.origin.y + self.dy) * SIZE > (580 - 30) * SIZE) {
            self.myView.center = CGPointMake(self.myView.frame.size.width / 2 + 8, 640 * SIZE);
        }
        
//        NSLog(@"**********%f", self.myView.frame.origin.y + self.dy);

    }
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.photos.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WJYPhotoCollectionViewCell *PhotoCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];
    PhotoCollectionViewCell.dic = [self.photos objectAtIndex:indexPath.item];
    
    NSDictionary *dic = [self.photos objectAtIndex:indexPath.item];
    NSString *str = [dic objectForKey:@"imgurl"];
    [self.backGroundImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"audionews_play_bg"]];
    
    // 判断imgtitle为空就用note的内容
    if ([[[self.photos objectAtIndex:indexPath.item] objectForKey:@"imgtitle"] isEqualToString:@""]) {
        self.textLabel.text = [[self.photos objectAtIndex:indexPath.item] objectForKey:@"note"];
    } else {
        self.textLabel.text = [[self.photos objectAtIndex:indexPath.item] objectForKey:@"imgtitle"];
    }
    
    self.rect = [self.textLabel.text boundingRectWithSize:CGSizeMake(SCREEN_FRAME.size.width - 2 * 5, 0)  options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    self.scroll.contentSize = CGSizeMake(0, self.rect.size.height);
    self.textLabel.frame = CGRectMake(0, 0, SCREEN_FRAME.size.width - 2 * 5, self.rect.size.height);
    
    self.num = indexPath.item + 1;
    self.numL.text = [NSString stringWithFormat:@"%ld / %ld", self.num, self.photos.count];
    
    return PhotoCollectionViewCell;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
